package de.stromhaltig.grid;

import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import java.util.Random;

import javax.swing.JDialog;
import javax.swing.JLabel;

import org.jivesoftware.smack.XMPPException;

public class Main implements ACStatusListener, Runnable{

	public static Properties properties = new Properties();
	
	public Main() {
		Thread thread = new Thread(this);
		thread.start();
	}
	
	public static void main(String[] argv) throws Exception{		
		XMPPConnector.getInstance().connect();
		
		Main.properties = new Properties();
		if(new File("gridswarm.properties").canRead() ) {
			FileReader fr = new FileReader("gridswarm.properties");
			
			Main.properties.load(fr);
			fr.close();
		} else {
			System.out.println("Register new Device");
			 final String alphabet = "QWERTUIPASDFGHKYXCNM123456789qwertupasdfghjk-";
			 final int N = alphabet.length();

			 Random r = new Random();
			 StringBuffer sb = new StringBuffer();
			    
		    for (int i = 0; i < 20; i++) {
		       sb.append(alphabet.charAt(r.nextInt(N)));
		    }
			
			properties.put("deviceid", sb.toString());
			 sb = new StringBuffer();
			    
			 for (int i = 0; i < 8; i++) {
			    	sb.append(alphabet.charAt(r.nextInt(N)));
			 }
			 properties.put("secret", sb.toString());
			 properties.put("xmppserver", "gridswarm.stromhaltig.de");
			 properties.put("xmppport", "5222");
			 properties.put("watt","40");
			 properties.put("master", "master@gridswarm.stromhaltig.de");
			 XMPPConnector.getInstance().register();
			 XMPPConnector.getInstance().connect();
			 FileWriter fw = new FileWriter("gridswarm.properties");
			 Main.properties.store(fw, "registered");
			 fw.close();
			 XMPPConnector.getInstance().connect();
		}
		
		DeviceDialog.getInstance().setVisible(true);
		XMPPConnector.getInstance().authenticate();
		XMPPConnector.getInstance().presenceConnect();		
	
		ACConnectMonitor.getInstance().addStatusListener(new Main());		
		XMPPConnector.getInstance().updatePresence(ACConnectMonitor.getInstance().onAC(), Integer.parseInt(Main.properties.getProperty("watt")));
		if(SystemTray.isSupported()) {
			TraySupport.addTrayIcon();
			
		}
	}

	@Override
	public void statusChangeEvent(boolean onAC) {
		XMPPConnector.getInstance().updatePresence(onAC, Integer.getInteger(Main.properties.getProperty("watt")));
		System.out.println("New Status:"+onAC);		
	}

	public static void saveProperties() {
		try {
		FileWriter fw = new FileWriter("gridswarm.properties");
		 Main.properties.store(fw, "registered");
		 fw.close();
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	@Override
	public void run() {
		while(true) {
		
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if((Main.properties!=null) &&(Main.properties.getProperty("watt")!=null)) {
			XMPPConnector.getInstance().updatePresence(ACConnectMonitor.getInstance().onAC(), Integer.parseInt(Main.properties.getProperty("watt")));
	}
		}
		
	}
}
